package app.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import app.dao.ApplicationDao;
import app.model.Application;

/** 
 * 
 * @author Nachman Mathieu
 * @author David Gianorio
 * @author Faton Alia
 * 
 * This class will manage all applications business logic methods */

@Component
public class ApplicationService {
	
	@Autowired ApplicationDao applicationDao;
	
	private static Set<Application> globalApplicationSet = new HashSet<Application>();

	
	//////////////////////////////////////////////////////////////
	////////////////// Method without Dao calls //////////////////
	//////////////////////////////////////////////////////////////
	
	/** Get the list of all applications stored in the Set */
	public List<Application> getAllApplications(){
		
		List<Application> allApplicationsList = new ArrayList<Application>();
		allApplicationsList.addAll(globalApplicationSet);
		
		return allApplicationsList;
	}
	
	/** Don't call this directly ... It will be called by upload method */ 
	public void addApplicationToSet(Application application){
		globalApplicationSet.add(application);
	}
	
	//////////////////////////////////////////////////////////////
	/////////////////// Method with Dao calls ////////////////////
	//////////////////////////////////////////////////////////////
	
	/** Get an application for the specified id */ // useless ????
	public Application getApplicationById(Long id){
		return (Application) applicationDao.find(id);
	}
	
	/** This method will load the map of all Applications on server startup */
	//@PostConstruct // Add this annotation when hbm2ddl will be set to "validate"
	public void loadGlobalApplicationSet(){
		for(Application app : applicationDao.getAllApplications()){
			globalApplicationSet.add(app);
		}
	}
}
